#!/bin/bash

source /home/shell/util.sh

function install () {
    if [ "$(ostype)" = "Ubuntu" ]; then
        while [ true ]; do
            curl -sL https://repo.mongodb.org/apt/ubuntu/dists/|grep -Po "<a href='.*'>.*</a>"|grep -v "Directory"|perl -pe "s/<a href='(.*)'>.*<\/a>/\1/g"
            read -p "请选择系统版本[$(lsb_release -a|grep "Codename"|perl -pe "s/Codename:\s*(.*)/\1/g")]：" os_version
            if [[ -z "${os_version}" ]]; then
                printFail "请输入版本"
            else
                break;
            fi
        done
        while [ true ]; do
            curl -sL https://repo.mongodb.org/apt/ubuntu/dists/${os_version}/mongodb-org/|grep -Po "<a href='\d+\.\d+'>.*</a>"|perl -pe "s/<a href='(.*)'>.*<\/a>/\1/g"
            read -p "请选择mongodb大版本：" app_version
            if [[ -z "${app_version}" ]]; then
                printFail "请输入版本"
            else
                break;
            fi
        done
        while [ true ]; do
            curl -sL https://repo.mongodb.org/apt/ubuntu/dists/${os_version}/mongodb-org/${app_version}/multiverse/binary-amd64/|grep -Po "mongodb-org-server_.*?_amd64.deb"|uniq|perl -pe "s/mongodb-org-server_(.*)_amd64.deb/\1/g"
            read -p "请选择mongodb小版本：" version
            if [[ -z "${version}" ]]; then
                printFail "请输入版本"
            else
                break;
            fi
        done

        while [ true ]; do
            curl -sL https://repo.mongodb.org/apt/ubuntu/dists/${os_version}/mongodb-org/${app_version}/multiverse/binary-amd64/|grep -Po "mongodb-mongosh.*?.deb"|uniq|perl -pe "s/mongodb-mongosh_(\d+\.\d+\.\d+)_amd64.deb/\1/g"
            read -p "请选择mongosh版本：" shell_version
            if [[ -z "${shell_version}" ]]; then
                printFail "请输入版本"
            else
                break;
            fi
        done
        
        dpkg-install-fromurl https://repo.mongodb.org/apt/ubuntu/dists/${os_version}/mongodb-org/${app_version}/multiverse/binary-amd64/mongodb-org-server_${version}_amd64.deb
        dpkg-install-fromurl https://repo.mongodb.org/apt/ubuntu/dists/${os_version}/mongodb-org/${app_version}/multiverse/binary-amd64/mongodb-mongosh_${shell_version}_amd64.deb
    else
        rpm -ivh https://repo.mongodb.org/yum/redhat/8/mongodb-org/4.4/x86_64/RPMS/mongodb-org-server-4.4.5-1.el8.x86_64.rpm
        dnf install -y compat-openssl10
        rpm -ivh https://downloads.mongodb.com/compass/mongosh-0.11.0-x86_64.rpm
    fi
}

function config () {
    firewall-addport 27017

    # 配置
    sed -i "s/bindIp: 127.0.0.1/bindIp: 0.0.0.0/g" /etc/mongod.conf

    systemctl enable --now mongod

    cp ~/.mongorc.js ~/.mongoshrc.js

    read -p "admin账户密码[admin]：" admin_passwd
    admin_passwd=${admin_passwd:-"admin"}


    mongosh admin --quiet --eval 'db.createUser({user: "admin",pwd: "'$admin_passwd'",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]});';
}

install && config



# 修改密码
# db.changeUserPassword("admin", "aaaaa")

# 创建只读用户
# db.createUser({user:"a",pwd:"a",roles:[{role:"read",db:"test"}]})

# 创建读写用户
# db.createUser({user:"b",pwd:"b",roles:[{role:"readWrite",db:"test"}]})

printInfo "=====================安装信息====================="
printInfo "客户端版本："`mongosh --version`
printInfo "服务端版本："`mongod --version|grep "db version "|awk '{print $3}'`
printInfo "admin用户：admin"
printInfo "admin密码：$admin_passwd"
printInfo "=====================安装信息====================="